<?php
/**
 * 导出全国包邮的商品
 * User: yiwan
 * Date: 2018/5/18
 * Time: 10:07
 */

ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\ShippingTempl;
use Biz\model\ShippingTemplAttr;
use Biz\model\GoodsBasicInfo;

//港澳台，新疆，西藏地区
$otherArea = ['710000', '810000', '820000', '650000', '540000',
    "712800", "710200", "712600", "710900", "711900", "710700", "710500", "711500", "710600", "712700", "712400", "710100", "712500", "710300", "710400", "711400", "711100", "710800", "711300", "711200", "712100", "711700",
    "810001", "810002", "810003", "810004", "810005", "810006", "810007", "810008", "810009", "810010", "810011", "810012", "810013", "810014", "810015", "810016", "810017", "810018",
    "820001", "820002", "820003", "820004", "820005", "820006", "820007", "820008",
    "650100", "650200", "650400", "650500", "652300", "652700", "652800", "652900", "653000", "653100", "653200", "654000", "654200", "654300", "659001", "659002", "659003", "659004", "659006",
    "540100", "540200", "540300", "540400", "540500", "542400", "542500"
];

//var_dump(array_diff(['340000','810000'], $otherArea));
//die;

$list = [];
$page = 1;
$pageSize = 800;
$fullTemplIds = [];
do {
    $limit = [($page - 1) * $pageSize, $pageSize];
    $where = ['LIMIT' => $limit];
    $list = ShippingTempl::select(['id'], ['status' => ShippingTempl::STATUS_PASSED, 'LIMIT' => $limit]);
    if ($list) {
        $templIds = array_column($list, 'id');
        $attrList = ShippingTemplAttr::select('*', ['status' => ShippingTemplAttr::STATUS_PASSED, 'templ_id' => $templIds]);
        $attrArr = [];
        foreach ($attrList as $row) {
            $attrArr[$row['templ_id']][] = $row;
        }
        foreach ($attrArr as $templId => $attrs) {
            $flag = true;
            foreach ($attrs as $row) {
                if ($flag) {
                    if ($row['base_price'] > 0 || $row['continued_price'] > 0) {
                        $area = $row['area'];
                        $area = explode(",", $area);
                        if ($area) {
                            //是否只有港澳台
                            if (array_diff($area, $otherArea)) {
                                $flag = false;
                            }
                        }
                    }
                }
            }
            if ($flag) {
                $fullTemplIds[] = $templId;
            }
        }
    }
    $page++;
} while ($list);


$filepath = "/tmp/freeShippingGoods.csv";

$fp = fopen($filepath, 'w');


$title = [
    '商品Id',
    '运费模板Id'
];
fputcsv($fp, $title);
$shippingTemplIds = [];
$page = 1;
$pageSize = 100;
$goodsList = [];
do {
    $start = ($page - 1) * $pageSize;
    $shippingTemplIds = array_slice($fullTemplIds, $start, $pageSize);
    $goodsList = GoodsBasicInfo::select(['goods_id', 'shipping_templ_id'], ['shipping_templ_id' => $shippingTemplIds, 'status' => GoodsBasicInfo::STATUS_ONLINE]);
    foreach ($goodsList as $row) {
        $item = [
            $row['goods_id'],
            $row['shipping_templ_id'],
        ];
        fputcsv($fp, $item);
    }
    $page++;
} while ($goodsList);

fclose($fp);


